package com.emex.app.stuff.gui.model;

import com.emex.app.shared.data.OrderPosition;
import com.emex.app.shared.data.Provider;
import com.emex.app.shared.data.enums.OrderPositionStatus;
import com.emex.app.stuff.provider.ProviderDataProvider;
import java.util.List;
import java.util.Objects;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey Karavaev
 */
public class OrderPositionTableModel extends AbstractTableModel
        implements TableModel, TableCrud<OrderPosition> {

    /**
     * List of all orderPositions.
     */
    private final List<OrderPosition> orderPositions;
    /**
     * Providers cache list.
     */
    private List<Provider> providers;
    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Наименование", "Кол-во", "Цена", "Закуп. цена", "Статус", "Поставщик"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        String.class, Integer.class, Double.class, Double.class, OrderPositionStatus.class, String.class
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public OrderPositionTableModel(List<OrderPosition> positions) {
        orderPositions = positions;
        providers = ProviderDataProvider.getInstance().getAllProviders();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return orderPositions.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if ((rowIndex >= 0) && (rowIndex < orderPositions.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return orderPositions.get(rowIndex);
                }
                case 0: {
                    return orderPositions.get(rowIndex).getName();
                }
                case 1: {
                    return orderPositions.get(rowIndex).getAmount();
                }
                case 2: {
                    return orderPositions.get(rowIndex).getPrice();
                }
                case 3: {
                    return orderPositions.get(rowIndex).getPurPrice();
                }
                case 4: {
                    return OrderPositionStatus.valueOf(orderPositions.get(rowIndex).getStatus());
                }
                case 5: {
                    return getProviderNameById(orderPositions.get(rowIndex).getProviderId());
                }
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public void dataRemoved(OrderPosition t) {
        orderPositions.remove(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataAdded(OrderPosition t) {
        if (!orderPositions.contains(t)) {
            orderPositions.add(t);
            this.fireTableDataChanged();
        }
    }

    @Override
    public void dataUpdated() {
        this.fireTableDataChanged();
    }

    protected String getProviderNameById(Integer id) {
        for (Provider provider : providers) {
            if (Objects.equals(provider.getId(), id)) {
                return provider.getName();
            }
        }
        return "";
    }
}
